import Vue from 'vue';
import App from './App.vue';
import store from './store/index';
import router from './router';
import { postKeyValueRequest } from './utils/api';
import { postRequest } from './utils/api';
import { putRequest } from './utils/api';
import { getRequest } from './utils/api';
import { deleteRequest } from './utils/api';
import { upload } from './utils/api';
import ElementUI from 'element-ui'; //引入Element ui
import { Message } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import mavonEditor from 'mavon-editor';
import 'mavon-editor/dist/css/index.css';
import './plugins/hljs';
import '@/css/normal.css';
// 引入vuex-store
// 使用 mavonEditor
Vue.use(mavonEditor);
Vue.config.productionTip = false;
Vue.use(ElementUI); //使用 ElementUI
Vue.prototype.postKeyValueRequest = postKeyValueRequest;
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.getUpload = upload;

router.beforeEach((to, from, next) => {
	//to: 将要去哪里  from: 从哪里来的  next: 下一步，放行
	//如果要去的页面是Login页，直接放行
	if (to.path == '/' || to.path == '/login') {
		next();
	} else {
		//如果有user，说明已经登录了
		if (window.sessionStorage.getItem('user')) {
			next();
		} else {
			//没有登录就去登录页并记录你刚才要去的路由
			//to.path是你刚要去的路由，但因未登录被拦截了，在登录之后就会直接去到你刚访问的路由
			Message.error({ message: '尚未登录，请先登录' });
			next({
				path: '/login'
			});
		}
	}
});

Vue.filter('dateFormat', function(originVal) {
	const dt = new Date(originVal);

	const y = dt.getFullYear();
	const m = (dt.getMonth() + 1 + '').padStart(2, '0');
	const d = (dt.getDate() + '').padStart(2, '0');

	const hh = (dt.getHours() + '').padStart(2, '0');
	const mm = (dt.getMinutes() + '').padStart(2, '0');
	const ss = (dt.getSeconds() + '').padStart(2, '0');

	return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
});

new Vue({
	store,
	router,
	render: (h) => h(App)
}).$mount('#app');
